package com.dgshanger.sanhxiaofeisc.ui.recharge

import android.annotation.SuppressLint
import android.content.Intent
import android.widget.ScrollView
import com.antiphon.paysdk.AntiphonPaySDK
import com.antiphon.paysdk.PayResultCallBack
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants.KEY_INTENT_VIP
import com.dgshanger.sanhxiaofeisc.common.Constants.KEY_INTENT_VIP_COUNT
import com.dgshanger.sanhxiaofeisc.entity.VipDetailEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.VipCardContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.VipCardPresenter
import com.dgshanger.sanhxiaofeisc.ui.WebActivity
import com.dgshanger.sanhxiaofeisc.ui.person.purse.MembershipCardActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import com.xxl.sodmall.dialog.WalletPayDialogWrapper
import kotlinx.android.synthetic.main.activity_vip_card.*
import java.math.BigDecimal


/**
 * VipCardActivity
 * 沉迷学习不能自拔
 * Describe：会员卡页面
 * Created by 雷小星🍀 on 2018/4/19 13:54.
 */
class VipCardActivity : BaseMvpLcecActivity<ScrollView, VipDetailEntity, VipCardContract.Model, VipCardContract.View, VipCardPresenter>(),
        VipCardContract.View {

    override fun startAliPay(info: String?) {
        AntiphonPaySDK.getIntance().init(this)//支付宝针对Activity初始化
        AntiphonPaySDK.getIntance().createAliPay(info, object : PayResultCallBack {
            override fun onSuccess() {
                showToast(getString(R.string.pay_success))
                startActivityThenFinishSelf(MembershipCardActivity::class.java)
            }

            override fun onFiale(p0: String?) {
                showToast(getString(R.string.Payment_error))
            }
        })
    }

    override fun startWeChatPay(info: String?) {
        AntiphonPaySDK.getIntance().createWxPay(info, object : PayResultCallBack {
            override fun onSuccess() {
                showToast(getString(R.string.pay_success))
                startActivityThenFinishSelf(MembershipCardActivity::class.java)
            }

            override fun onFiale(p0: String?) {
                showToast(getString(R.string.Payment_error))
            }

        })
    }

    override fun dismissPayDialog() {
        payDialogWrapper?.dismissDialog()
    }

    companion object {
        val PAY_WX = "pay_wx"
        val PAY_ALIPAY = "pay_alipay"
        val PAY_WALLET = "pay_wallet"
    }

    override fun isAgreed(): Boolean = cbAgreed.isChecked

    override fun getVipCount(): String = count.toString()

    override fun getVipId(): String = intent.getStringExtra(KEY_INTENT_VIP)

    override fun createPresenter(): VipCardPresenter = VipCardPresenter()

    override fun loadData(refresh: Boolean) {
        presenter.loadVipDetail()
    }

    private lateinit var data: VipDetailEntity

    @SuppressLint("SetTextI18n")
    override fun setData(data: VipDetailEntity?) {
        showContent()
        with(data!!) {
            this@VipCardActivity.data = this
            tv_level_name.text = "${level_name}x1"
            tv_dscp.text = dscp
            tv_valid_time.text = "${valid_time}个月"
            tv_distribute_rate.text = "${distribute_rate}%"
            tv_return_wallet_money.text = return_wallet_money
            tv_return_integral_num.text = return_integral_num
            tv_recharge_money.text = getString(R.string.Now_you_only_have_to_pay) + recharge_money + "元"
        }
    }

    //购买协议
    override fun onGetBuyAgreement(data: String?) {
        val intent = Intent(mContext, WebActivity::class.java)
        intent.putExtra("data", data)
        intent.putExtra("title", resources.getString(R.string.tv_purchase_agreement))
        startActivity(intent)
    }

    override fun getActivityLayoutId(): Int = R.layout.activity_vip_card

    override fun initData() {
        super.initData()
        loadData(true)
    }

    private var count: Int = 1

    @SuppressLint("SetTextI18n")
    override fun initEvent() {
        //购买协议
        tv_pro.setOnClickListener {
            presenter.getBuyAgreement()
        }



        iv_add.setOnClickListener {
            //加
            count++
            changeUI()
        }
        iv_subtract.setOnClickListener {
            //减
            if (count > 1) {
                count--
                changeUI()
            }
        }

        view_wallet_pay.setOnClickListener {
            //零钱支付
            if (!isAgreed) {
                showToast(getString(R.string.Please_review_and_agree_to_purchase_agreement))
            } else {
                presenter.checkPayPassword()
            }
        }
        view_wechat_pay.setOnClickListener {
            //微信支付
            presenter.vipBuy(PAY_WX)
        }
        view_ali_pay.setOnClickListener {
            //支付宝支付
            presenter.vipBuy(PAY_ALIPAY)
        }
        cardView_pay_for.setOnClickListener {
            if (!isAgreed) {
                showToast(getString(R.string.Please_review_and_agree_to_purchase_agreement))
                return@setOnClickListener
            }
            //他人代付
            val intent = Intent(mContext, PayForActivity::class.java)
            intent.putExtra(KEY_INTENT_VIP, vipId)
            intent.putExtra(KEY_INTENT_VIP_COUNT, count)
            startActivity(intent)
        }
    }

    private var payDialogWrapper: WalletPayDialogWrapper? = null

    override fun showPayDialog() {
        val payDialogWapper = WalletPayDialogWrapper(mContext, data.recharge_money
                .run {
                    BigDecimal(toDouble())
                            .multiply(BigDecimal(count))
                            .toString()
                })
                .setOnPasswordFinishListener(object : WalletPayDialogWrapper.OnPasswordFinishListener {
                    override fun onPasswordFinishListener(password: String) {
                        presenter.vipBuy(PAY_WALLET, password)
                    }
                })
        payDialogWapper.showDialog()

    }

    @SuppressLint("SetTextI18n")
    private fun changeUI() {
        tv_level_name.text = "${data.level_name}x${count}"
        tv_return_wallet_money.text = data.return_wallet_money
                .run {
                    BigDecimal(toDouble())
                            .multiply(BigDecimal(count))
                            .toString()
                }
        tv_return_integral_num.text = data.return_integral_num
                .run {
                    BigDecimal(toDouble())
                            .multiply(BigDecimal(count))
                            .toString()
                }
        tv_recharge_money.text = "现仅需：${data.recharge_money
                .run {
                    BigDecimal(toDouble())
                            .multiply(BigDecimal(count))
                            .toString()
                }}元"
    }
}